/**
 * 
 */
$(document).ready(function() {
	var wasProfileList=uft.getProfileByType("WebSphere")
	if( !jQuery.isEmptyObject(wasProfileList)){
		dwr.util.addOptions("profileListSelect",wasProfileList, "profileName")
	}
	$( "#profileListSelect" ).change(function() {
		if ( $("#profileListSelect").val() != ""){
			var exist=$.grep(wasProfileList, function(obj) {
				return obj.profileName === $("#profileListSelect").val()
			});
			if( !jQuery.isEmptyObject(exist)){
				var profileObj=exist[0]
				dwr.util.removeAllRows("wasConfigServerListTableTbody");
				var serverList=uft.getWasConfigurationServerList(profileObj.id);
				dwr.util.addRows( "wasConfigServerListTableTbody",serverList,[function(data) { return data.serverName; }],{ escapeHtml:false });
			}
		}
	})
	
	$('#wasConfigServerListTable tbody').on('click','tr', function () { 
		if(!$(this).hasClass('selected')){ 
			$("#wasConfigServerListTable tbody tr").removeClass('selected');
			$(this).addClass('selected');
			var serverName =$(this).text();
			//var profileName= $("#profileListSelect").val();
			var exist=$.grep(wasProfileList, function(obj) {
				return obj.profileName === $("#profileListSelect").val()
			});
			if( !jQuery.isEmptyObject(exist)){
				var profileObj=exist[0]
				$("#wasConfigurationDetails").atf_display_loader("Loading Server Details...")
				uft.updateHrefDiv("wasConfigurationDetails", "jsp/wasConfigurationDetails.jsp?profileId="+profileObj.id+"&serverName="+serverName)
			}
		}
	})
	$("#refreshWasServerList").click(function(){
		var exist=$.grep(wasProfileList, function(obj) {
				return obj.profileName === $("#profileListSelect").val()
		});
		if( !jQuery.isEmptyObject(exist)){
			var profileObj=exist[0]
			dwr.util.removeAllRows("wasConfigServerListTableTbody");
			var serverList=uft.getWasConfigurationServerList(profileObj.id);
			dwr.util.addRows( "wasConfigServerListTableTbody",serverList,[function(data) { return data.serverName; }],{ escapeHtml:false });
		}
	})
	$( "#addServerConfigModalSvBtn" ).click(function(){
		var serverName =$("#sName").val();
		//var profileName= $("#profileListSelect").val();
		var exist=$.grep(wasProfileList, function(obj) {
			return obj.profileName === $("#profileListSelect").val()
		});
		if( !jQuery.isEmptyObject(exist)){
			var profileObj=exist[0]
			uft.addServerConfigToProfile(profileObj.id, serverName,"addServerConfigModal-alert-container");
		}
	})
	$( "#removeWasServer" ).click(function(){
		var serverName=$("#wasConfigServerListTable tbody").find(".selected").text();
		var exist=$.grep(wasProfileList, function(obj) {
			return obj.profileName === $("#profileListSelect").val()
		});
		if( !jQuery.isEmptyObject(exist)){
			var profileObj=exist[0]
			console.log("Profileid: "+profileObj.id+" ServerName: "+serverName)
			uft.removeServerConfigFileProfile(profileObj.id, serverName,"addServerConfigModal-alert-container");
		}
	})
});